package in.muton.edushare.academics.alerts;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.grading.panels.studentGrades.GradingUtil.ItemGradeHelper;
import in.muton.edushare.processing.objects.CourseDataHandler;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.OutgoingEmailsDAO;
import com.edushare.common.mailer.FacultyGradeUpdate;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Users;

public class FacultyGradeMail {
	private IModel<Users> loggedInUser;
	private List<ItemGradeHelper> ighList = new ArrayList<ItemGradeHelper>();
	private IModel<CourseInstances> cInst;

	public FacultyGradeMail(IModel<CourseInstances> cInst,
			IModel<Users> loggedInUser) {
		this.loggedInUser = loggedInUser;
		this.cInst = cInst;
	}

	public void addAlert(ItemGradeHelper helper) {
		ighList.add(helper);
	}

	public void sendAlerts() {
		String gradeMsg = "";
		boolean first = true;
		for (ItemGradeHelper igh : ighList) {
			gradeMsg += gradeMsg1;
			if (first) {
				gradeMsg += igh.gradingItem.getObject().getGradingItemName();
				first = false;
			} else {
				gradeMsg += "&nbsp;";
			}
			gradeMsg += gradeMsg2;
			gradeMsg += igh.user.getObject().getCompleteUserName();
			gradeMsg += gradeMsg3;
			gradeMsg += igh.marks + " of "
					+ igh.gradingItem.getObject().getMaxMarks();
			gradeMsg += gradeMsg4;
		}
		List<Users> facList = new CourseDataHandler(cInst).getTeachers()
				.getUtilList();
		if (!facList.contains(EduShareSession.get().getLoggedInUser())) {
			facList.add(EduShareSession.get().getLoggedInUser());
		}
		for (Users u : facList) {
			new OutgoingEmailsDAO(EduShareSession.get()
					.getSessionORMInterface()).sendMail(new FacultyGradeUpdate(
					gradeMsg, cInst.getObject(), u, loggedInUser.getObject())
					.send());
		}
	}

	private static final String gradeMsg1 = "<div style=\"clear:both;padding-top:5px;\">"
			+ "<div style=\"float:left;width:190px;text-align:left;\">";
	private static final String gradeMsg2 = "</div>"
			+ "<div style=\"float:left;width:190px;text-align:left;\">";
	private static final String gradeMsg3 = "</div>"
			+ "<div style=\"float:left;width:140px;text-align:left;\">";
	private static final String gradeMsg4 = "</div>" + "</div>";
}
